<?php
/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 2018/4/17
 * Time: 20:13
 */

namespace app\home\controller;
use app\home\model\Word as WordModel;
use app\home\model\WechatUser;
use think\Db;

class Word extends Base
{
    public function detail(){
        $userId = session('userId');
        $User = WechatUser::where(['openid' => $userId])->field('subscribe,review_status,openid,role')->find();
        if ($User['review_status'] == 0 || $User['role'] == 1 || $User['subscribe'] == 0){
            $this->redirect('Journal/null');
        }
        $this->jssdk();
        $id = input('id');
        $learnModel = new WordModel();
        $article = $learnModel::get($id);
        if ($article['status'] == -1 || empty($article)){
            $this->redirect('Journal/del');
        }
        $res = Db::name('word_sign')->where(['openid' => $userId,'type' => $article['type']])->find();
        if (empty($res)){
            // 没有记录  存储
            Db::name('word_sign')->insert(['type' => $article['type'],'openid' => $userId,'day' => $article['day'],'create_time' => time()]);
        }else{
            // 有记录 更新
            if ($res['day'] < $article['day']){
                // 同一条数据
                Db::name('word_sign')->where('id',$res['id'])->update(['day' => $article['day'],'create_time' => time()]);
            }
        }
        if ($article['record1']){
            $article['record1'] = "/DDDeducation/public".$article['record1'];
        }
        if ($article['record2']){
            $article['record2'] = "/DDDeducation/public".$article['record2'];
        }
        if ($article['record3']){
            $article['record3'] = "/DDDeducation/public".$article['record3'];
        }
        $this->assign('article',$article);
        return $this->fetch();
    }
    /**
     * 历史记录
     */
    public function news(){
        $userId = session('userId');
        $User = WechatUser::where(['openid' => $userId])->field('subscribe,review_status,openid,role')->find();
        if ($User['review_status'] == 0 || $User['role'] == 1 || $User['subscribe'] == 0){
            $this->redirect('Journal/null');
        }
        $this->jssdk();
        $res = Db::name('word_sign')->where(['openid' => $userId])->field('type,day')->find();
        $list = [];
        if ($res){
            $list = WordModel::where(['type' => $res['type'],'day' => ['<=',$res['day']]])->order('day','desc')->field('id,front_cover,title')->limit(0,10)->select();
            foreach ($list as $value){
                $img = get_cover($value['front_cover'],'path');
                $value['front_cover'] = "/DDDeducation/public".$img;
            }
        }
        $this->assign('list',$list);
        $this->assign('type',$res['type']);
        $this->assign('day',$res['day']);
        return $this->fetch();
    }
    /**
     * 更多
     */
    public function more(){
        $day = input('day');
        $len = input('length');
        $type = input('type');
        $list = WordModel::where(['type' => $type,'day' => ['<=',$day]])->order('day','desc')->field('id,front_cover,title')->limit($len,10)->select();
        foreach ($list as $value){
            $img = get_cover($value['front_cover'],'path');
            $value['front_cover'] = "/DDDeducation/public".$img;
        }
        if ($list){
            return ['code' => 1 ,'msg' => '加载成功','data' => $list];
        }else{
            return ['code' => 0 ,'msg' => '加载失败'];
        }
    }
}